package com.base.cn.platform.os.controller.user.signin;

import com.base.cn.platform.os.common.constants.CacheConstants;
import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.j2cache.CustomCacheChannel;
import com.base.cn.platform.os.common.j2cache.J2CacheRegion;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.action.ActionKey;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.user.integral.IntegralRule;
import com.base.cn.platform.os.entity.user.signin.CusUserSignIn;
import com.base.cn.platform.os.service.user.integral.CusUserIntegralBiz;
import com.base.cn.platform.os.service.user.integral.IntegralRuleBiz;
import com.base.cn.platform.os.service.user.signin.CusUserSignInBiz;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 用户签到Controller
 * @author ll
 * @date 2018/5/25 14:20
 */
@RestController
@RequestMapping("/manage/user")
public class CusUserSignInController extends BaseController {
    @Autowired
    private CusUserSignInBiz cusUserSignInBiz;
    @Autowired
    private CusUserIntegralBiz cusUserIntegralBiz;
    @Autowired
    private IntegralRuleBiz integralRuleBiz;
    @Autowired
    private CustomCacheChannel customCacheChannel;

    /**
     * 用户签到的总天数
     * @param userId 用户ID
     * @return BigDecimal
     */
    @RequestMapping("/totalSignInNum")
    public BigDecimal totalSignInNum(@RequestParam("userId") BigDecimal userId){
        return cusUserSignInBiz.count(" userId="+userId);
    }

    /**
     * 保存签到
     * @param userSignIn
     * @return
     */
    @RequestMapping("/saveUserSignIn")
    public Map<String,Object> saveUserSignIn(@RequestBody  CusUserSignIn userSignIn){
        if(!ObjectUtils.isNotEmpty(userSignIn)){
            return ResultUtil.ERROR("添加数据不能为空");
        }
        if(ObjectUtils.isEmpty(userSignIn.getSignDate())){
            return ResultUtil.ERROR("签到时间不能为空");
        }else{
            List<CusUserSignIn> userSignIns = cusUserSignInBiz.selectUserSignInList(userSignIn);
            if(ObjectUtils.isNotEmpty(userSignIns) && userSignIns.size()>0){
                return ResultUtil.ERROR("已经签到过");
            }
        }
        userSignIn =cusUserSignInBiz.saveUserSignIn(userSignIn);
        // 签到获取积分
        cusUserIntegralBiz.addUserIntegralData(ActionKey.SIGNIN, userSignIn.getUserId(), null, 0, 2);
        IntegralRule integralRule = integralRuleBiz.getIntegralRuleByKey(ActionKey.SIGNIN);
        Integer score = Integer.valueOf(integralRule.getContextTreeMap().get("score"));
        Object s =  customCacheChannel.get(J2CacheRegion.THIRTY_SIX_HOURS_REGION.toString(), CacheConstants.SIGN_COUNT+userSignIn.getUserId()).getValue();
        int signCount =0;
        if(!StringUtils.isEmpty(s)){
            signCount= Integer.parseInt(s.toString());
        }
        signCount+=1;
        Long aLong = DateUtils.currentToTwentyFour();
        J2CacheRegion.THIRTY_SIX_HOURS_REGION.setValue("10000, "+aLong+"s");
        customCacheChannel.set(J2CacheRegion.THIRTY_SIX_HOURS_REGION.toString(), CacheConstants.SIGN_COUNT+userSignIn.getUserId(), signCount);
        userSignIn.setScore(score);
        userSignIn.setDay(signCount);
        integralRule = integralRuleBiz.getIntegralRuleByKey(ActionKey.NAME);
        TreeMap<String, String> contextTreeMap = integralRule.getContextTreeMap();
        userSignIn.setAlias(contextTreeMap.get("name"));
        return ResultUtil.SUCCESS("自动签到成功，祝你学习愉快！",userSignIn);
    }

    /**
     * 搜索签到列表
     * @param userSignIn
     * @return
     */
    @RequestMapping("/selectUserSignInList")
    public List<CusUserSignIn> selectUserSignInList(@RequestBody  CusUserSignIn userSignIn){
        return cusUserSignInBiz.selectUserSignInList(userSignIn);
    }

    /**
     * 验证用户当天是否签到过
     * @param userId 用户ID
     * @param signDate 签到时间
     * @return true签到过，false未签到过
     */
    @RequestMapping("/checkUserIsSign")
    public boolean checkUserIsSign(@RequestParam("userId") BigDecimal userId,
                                   @RequestParam("signDate") String signDate){
        CusUserSignIn userSignIn = new CusUserSignIn();
        userSignIn.setUserId(userId);
        userSignIn.setSignDate(DateUtils.parseDate(signDate,"yyyy-MM-dd"));
        List<CusUserSignIn> signInList = cusUserSignInBiz.selectUserSignInList(userSignIn);
        if(ObjectUtils.isEmpty(signInList)){
            return false;
        }
        return true;
    }

    /**
     * 用户Id搜索用户签到记录
     * @param userId 用户ID
     * @return List<CusUserSignIn>
     */
    @RequestMapping("/querySignInDateByUserId")
    public List<CusUserSignIn> querySignInDateByUserId(@RequestParam("userId") BigDecimal userId){
        return cusUserSignInBiz.querySignInDateByUserId(userId);
    }

    /**
     * 获取用户的某个月的签到记录
     * @param userId 用户ID
     * @param year 年
     * @param month 月
     * @return List<CusUserSignIn>
     */
    @RequestMapping("/findUserMonthSignList")
    public List<CusUserSignIn> findUserMonthSignList(@RequestParam("userId") BigDecimal userId,
                                                     @RequestParam("year") String year,
                                                     @RequestParam("month") String month){
        if(StringUtils.isEmpty(year) || year.equals("null")){
            year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
        }
        if(StringUtils.isEmpty(month) || month.equals("null")){
            month = String.valueOf(Calendar.getInstance().get(Calendar.MONTH)+1);
        }
        return cusUserSignInBiz.findUserMonthSignList(userId,year,month);
    }

}
